<?php
/**
 * Polling.php
 *
 * @copyright   2023 thingsworld.com.cn - All Rights Reserved
 * @link        http://thingsworld.com.cn/
 * @author      stiffer.chen <chenl@thingsworld.com.cn>
 * @created     2023-09-02 11:52:14
 * @modified    2023-09-02 11:52:14
 *
 */

namespace App\Models;

use App\Models\Polling\History;
use App\Models\Polling\Image;
use App\Repositories\PollingRepo;
use Carbon\Carbon;

class Polling extends BaseModel
{
    protected $table = 'polling';
    protected $primaryKey = 'id';

    public const CREATED_AT = 'create_time';
    public const UPDATED_AT = 'update_time';

    protected $fillable = [
        'number',
        'type',
        'status',
        'customer_id',
        'customer_name',
        'customer_user_id',
        'customer_user_name',
        'customer_user_telephone',
        'customer_address',
        'customer_sign',
        'plan_at',
        'description',
        'user_id',
        'user_name',
        'create_user_id',
        'create_user_name',
        'acted',
        'exception',
        'start_at',
        'complete_at',
        'complete_description',
        'timeout_hour',
        'mark'
    ];

    protected $appends = [
        'status_text',
        'is_timeout',
        'btn_delete',
        'btn_accept',
        'btn_start',
        'btn_complete'
    ];

    public function getBtnCompleteAttribute()
    {
        $userId = registry('user_id');
        if ($userId != $this->user_id) {
            return false;
        }
        if ($this->status != PollingRepo::STATUS_START) {
            return false;
        }
        return true;
    }

    public function getBtnStartAttribute()
    {
        $userId = registry('user_id');
        if ($userId != $this->user_id) {
            return false;
        }
        if ($this->status != PollingRepo::STATUS_ACCEPT) {
            return false;
        }
        return true;
    }

    public function getBtnAcceptAttribute()
    {
        $userId = registry('user_id');
        if ($userId != $this->user_id) {
            return false;
        }
        if ($this->status != PollingRepo::STATUS_ASSIGN) {
            return false;
        }
        return true;
    }

    public function getBtnDeleteAttribute()
    {
        $userId = registry('user_id');
        if ($userId != $this->create_user_id) {
            return false;
        }
        if ($this->status == PollingRepo::STATUS_COMPLETE) {
            return false;
        }
        return true;
    }

    public function getStatusTextAttribute()
    {
        $status =  $this->status;
        return PollingRepo::getStatusText($status);
    }

    public function getIsTimeoutAttribute()
    {
        $timeoutHour = floatval($this->timeout_hour);
        $status = $this->status;
        $planAt = $this->plan_at;
        $now = Carbon::now()->toDateTimeString();
        if (Carbon::parse($planAt)->addHours($timeoutHour)->toDateTimeString() <= $now && $status != PollingRepo::STATUS_COMPLETE) {
            return true;
        }
        return false;
    }

    public function scopeByNumber($query, $number)
    {
        return $query->where('number', $number);
    }

    public function histories()
    {
        return $this->hasMany(History::class);
    }

    public function images()
    {
        return $this->hasMany(Image::class);
    }
}